import os
import json
import requests
import struct
import threading
import time
#-----------------------------
# install modbus libs
# pip3 install  pymodbus==2.5.3  --低版本
# pip3 install -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple paho-mqtt
#-----------------------------
from pymodbus.client.sync import ModbusSerialClient as ModbusClient
import paho.mqtt.client as mqtt 
#-----------------------------
# pip install pymysql
# pip install pymssql
# pip install DBUtils==1.3  --- 特别要注意 
#-----------------------------
import pymysql
import pymssql
from pymysql.constants import CLIENT
from DBUtils.PooledDB import PooledDB
import os

print('modbus rtu terminal...')
'''
pip install -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple pymodbus==2.5.3
pip install -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple paho-mqtt
pip install -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple pymysql
pip install -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple pip install DBUtils==1.3
pip install -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple pymssql
'''

comPort = ""
comBaudrate = 38400
comDataBit = 8
comStopBit = 1
comParity = 'E'
unitId = 1 
regList = [{"regStart":30000,"regLen":15},{"regStart":30015,"regLen":10},{"regStart":30025,"regLen":10},{"regStart":30070,"regLen":1},{"regStart":30071,"regLen":1},{"regStart":30072,"regLen":1},{"regStart":30073,"regLen":2},{"regStart":30075,"regLen":2},{"regStart":30077,"regLen":2},{"regStart":30079,"regLen":2},{"regStart":30081,"regLen":2},{"regStart":32000,"regLen":1},{"regStart":32002,"regLen":1},{"regStart":32003,"regLen":1},{"regStart":32008,"regLen":1},{"regStart":32009,"regLen":1},{"regStart":32010,"regLen":1},{"regStart":32016,"regLen":1},{"regStart":32017,"regLen":1},{"regStart":32018,"regLen":1},{"regStart":32019,"regLen":1},{"regStart":32020,"regLen":1},{"regStart":32021,"regLen":1},{"regStart":32022,"regLen":1},{"regStart":32023,"regLen":1},{"regStart":32024,"regLen":1},{"regStart":32025,"regLen":1},{"regStart":32026,"regLen":1},{"regStart":32027,"regLen":1},{"regStart":32028,"regLen":1},{"regStart":32029,"regLen":1},{"regStart":32030,"regLen":1},{"regStart":32031,"regLen":1},{"regStart":32032,"regLen":1},{"regStart":32033,"regLen":1},{"regStart":32034,"regLen":1},{"regStart":32035,"regLen":1},{"regStart":32036,"regLen":1},{"regStart":32037,"regLen":1},{"regStart":32038,"regLen":1},{"regStart":32039,"regLen":1},{"regStart":32040,"regLen":1},{"regStart":32041,"regLen":1},{"regStart":32042,"regLen":1},{"regStart":32043,"regLen":1},{"regStart":32044,"regLen":1},{"regStart":32045,"regLen":1},{"regStart":32046,"regLen":1},{"regStart":32047,"regLen":1},{"regStart":32048,"regLen":1},{"regStart":32049,"regLen":1},{"regStart":32050,"regLen":1},{"regStart":32051,"regLen":1},{"regStart":32052,"regLen":1},{"regStart":32053,"regLen":1},{"regStart":32054,"regLen":1},{"regStart":32055,"regLen":1},{"regStart":32064,"regLen":2},{"regStart":32066,"regLen":1},{"regStart":32067,"regLen":1},{"regStart":32068,"regLen":1},{"regStart":32069,"regLen":1},{"regStart":32070,"regLen":1},{"regStart":32071,"regLen":1},{"regStart":32072,"regLen":2},{"regStart":32074,"regLen":2},{"regStart":32076,"regLen":2},{"regStart":32078,"regLen":2},{"regStart":32080,"regLen":2},{"regStart":32082,"regLen":2},{"regStart":32084,"regLen":1},{"regStart":32085,"regLen":1},{"regStart":32086,"regLen":1},{"regStart":32087,"regLen":1},{"regStart":32088,"regLen":1},{"regStart":32089,"regLen":1},{"regStart":32090,"regLen":1},{"regStart":32091,"regLen":2},{"regStart":32093,"regLen":2},{"regStart":32106,"regLen":2},{"regStart":32114,"regLen":2},{"regStart":35300,"regLen":1},{"regStart":35301,"regLen":2},{"regStart":35303,"regLen":1},{"regStart":35304,"regLen":1},{"regStart":35305,"regLen":2},{"regStart":35307,"regLen":1},{"regStart":40000,"regLen":2},{"regStart":40037,"regLen":1},{"regStart":40038,"regLen":1},{"regStart":40120,"regLen":1},{"regStart":40122,"regLen":1},{"regStart":40123,"regLen":1},{"regStart":40125,"regLen":1},{"regStart":40126,"regLen":2},{"regStart":40129,"regLen":2},{"regStart":40196,"regLen":1},{"regStart":40198,"regLen":1},{"regStart":40200,"regLen":1},{"regStart":40201,"regLen":1},{"regStart":42000,"regLen":1},{"regStart":42015,"regLen":2},{"regStart":42017,"regLen":2},{"regStart":42019,"regLen":2},{"regStart":42405,"regLen":2},{"regStart":43006,"regLen":1},{"regStart":45086,"regLen":1}]
client = ModbusClient( method="rtu" , port= comPort , baudrate=comBaudrate , stopbits=comStopBit, parity= comParity, bytesize=comDataBit, timeout=2 )
print('connect terminal')
client.connect()
print('connect success')
while (1>0) :
    print('read register data')
    time.sleep(2)
    for reg in regList:
        try:
            addr = reg['regStart']
            regNum = reg['regLen']
            result = client.read_holding_registers(addr, regNum, unit=unitId)
            if not result.isError():
                print("读取成功！")
                for i in range(regNum):
                    print(f"寄存器{addr+i}: {result.registers[i]}")
                else:
                    print("读取失败！")
        except Exception as eer:
            print(eer)

